package com.itboye.sunsun.person.adapter;

import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.OrderBean.InnerDetailBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.ApplyCustomActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SendActivityAdapter extends BaseAdapter {

	
	private List<InnerDetailBean>list;
	Context context;
	
	
	 public SendActivityAdapter(List<InnerDetailBean>list,Context context) {
		this.list=list;
		this.context=context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return  list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		
		ViewHolder holder=null;
		if(convertView==null)
		{
			holder=new ViewHolder();
			convertView=LayoutInflater.from(context).inflate(R.layout.listview_item_sendactivityadapter, null);

			holder.goods = (ImageView) convertView.findViewById(R.id.goods);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.count = (TextView) convertView.findViewById(R.id.count);
			holder.guige = (TextView) convertView.findViewById(R.id.guige);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.shouhou=(Button) convertView.findViewById(R.id.shouhou);
			holder.payrela = (RelativeLayout) convertView.findViewById(R.id.payrela);
			convertView.setTag(holder);
		}else
		{
			holder=(ViewHolder) convertView.getTag();
		}
		holder.name.setText(list.get(position).getName());
		holder.guige.setText(list.get(position).getSkuDesc());
		holder.price.setText(list.get(position).getPrice());
		XImageLoader.load(list.get(position).getImgUrl(), holder.goods);
		holder.count.setText("x" + list.get(position).getCount());
		final String orderCode=list.get(position).getOrderCode();
		final String price=list.get(position).getPrice();
		holder.shouhou.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(App.ctx,ApplyCustomActivity.class);
				intent.putExtra("ordercode", orderCode);
				intent.putExtra("price", price);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
				App.ctx.startActivity(intent);
				
				//shouhou(orderCode);
				
			}
		});
		
		
		//只在最后一行显示售后按钮
		if(position==(list.size()-1))
		{
			holder.payrela.setVisibility(View.VISIBLE);
		}else
		{
			holder.payrela.setVisibility(View.GONE);
		}
		
		return convertView;
	}
	final class ViewHolder
	{
		ImageView goods;

		RelativeLayout payrela;
		Button shouhou;
		TextView name;
		TextView count;
		TextView guige;
		TextView price;
	}
	public void shouhou(String orderCode) {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_Orders_afterSale")
				.param("order_code", orderCode)
				.param("uid", SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {

						Toast.makeText(App.ctx, response.toString(), 0).show();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, ""+msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}


}
